import java.text.SimpleDateFormat;
import java.util.Date;


public class ThreadLocalTest {
	private SimpleDateFormat ds;
	private static ThreadLocal jdbcContext;

	private ThreadLocalTest(SimpleDateFormat ds){
		this.ds = ds;
	}
	public static ThreadLocalTest getJdbcContext(SimpleDateFormat ds)
	{  
		if(jdbcContext==null)jdbcContext=new JDBCContextThreadLocal(ds);
		ThreadLocalTest context = (ThreadLocalTest) jdbcContext.get();
		if (context == null) {
			context = new ThreadLocalTest(ds);
		}

		return context;
	}
	
	private static class JDBCContextThreadLocal extends ThreadLocal {
		public SimpleDateFormat ds;
		public JDBCContextThreadLocal(SimpleDateFormat ds)
		{
			this.ds=ds;
		}
		protected synchronized Object initialValue() {
			return new ThreadLocalTest(ds);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Date myDate = new Date();
		System.out.println("current date:" + myDate);
		System.out.println("begin SimpleDateFormat:" + System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("end1 SimpleDateFormat:" + System.currentTimeMillis());
		
		String strFormatDate = sdf.format(myDate);
		System.out.println("end SimpleDateFormat:" + System.currentTimeMillis());
		System.out.println("current format date:" + strFormatDate);
		System.out.println("end2 SimpleDateFormat:" + System.currentTimeMillis());
		
		new Thread() {
			public void run() {
				int i = 0;
				try {
					while(true) {
						System.out.println("index:" + i++);
						Thread.sleep(30);
					}
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				
			}
		}.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Exit system");
		System.exit(0);
		
	}
}
